package com.esirong.whatime.ui.activity;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.esirong.dao.Task;
import com.esirong.whatime.R;
import com.esirong.whatime.mvp.BaseView;
import com.esirong.whatime.presenter.AddTaskPresenter;
import com.esirong.whatime.presenter.IAddTaskView;
import com.esirong.whatime.ui.fragment.SelectTaskTypeFragment;
import com.esirong.whatime.util.Logger;

public class AddTaskActivity extends BaseView<AddTaskPresenter> implements IAddTaskView, View.OnClickListener {

    private Task taskInfo = new Task();
    private Button mType;
    private EditText mTitle;
    private Button mAddress;
    private Button mLabels;
    private Button mGoals;
    private  Button mTimes;
    private TextView mContext;
    private TextView mLabel;
    private TextView mGoal;
    // 开如时间
    private TextView mStartAt;
    // 结束时间
    private TextView mEndAt;
    // 备注
    private TextView memo_tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_task);
        initView();

    }

    @Override
    protected AddTaskPresenter createPresenter() {
        return new AddTaskPresenter();
    }

    private void initView() {
        //
        mType = (Button) findViewById(R.id.edit_type_btn);
        mTitle = (EditText) findViewById(R.id.edit_title);
        mAddress = (Button) findViewById(R.id.edit_context_btn);
        mLabels = (Button) findViewById(R.id.edit_label_btn);
        mTimes = (Button) findViewById(R.id.edit_time_btn);
        mGoals = (Button) findViewById(R.id.edit_goal_btn);
        //
        mContext = findView(R.id.edit_context_tv);
        mLabel = findView(R.id.edit_labels_tv);
        mGoal =  findView(R.id.edit_goals_tv);

        mStartAt = (TextView) findViewById(R.id.edit_time_start_tv);
        mEndAt = (TextView) findViewById(R.id.edit_time_end_tv);
        memo_tv = (TextView) findViewById(R.id.memo_tv);
        findViewById(R.id.save_btn).setOnClickListener(this);
        mType.setOnClickListener(this);
        mTitle.setOnClickListener(this);

        mAddress.setOnClickListener(this);
        mLabels.setOnClickListener(this);
        mTimes.setOnClickListener(this);
        mGoals.setOnClickListener(this);
        memo_tv.setOnClickListener(this);
        mStartAt.setOnClickListener(this);
        mEndAt.setOnClickListener(this);
        mContext.setOnClickListener(this);
        mLabel.setOnClickListener(this);
        mGoal.setOnClickListener(this);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_task, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.save_btn:
                saveTask();
            break;
            case R.id.cancel_btn:
                cancel();
                break;
            case R.id.edit_type_btn:
                showTypeWindow();
                break;
            case R.id.edit_context_btn:
                showContentWindow();
                break;
            case R.id.edit_label_btn:
                showLabelWindow();
                break;
            case R.id.edit_time_start_tv:
                showTimeWindow();
                break;
            case R.id.edit_time_end_tv:
                showTimeWindow();
                break;
            case R.id.edit_goal_btn:
                showGoalWindow();
                break;

        }
    }

    private void showGoalWindow() {
        Logger.i(this.getLocalClassName(),"去设置目标");
    }

    private void showLabelWindow() {
        Logger.i(this.getLocalClassName(),"去设置标签");
    }

    private void showTimeWindow() {
        taskInfo.setStart_at(SystemClock.currentThreadTimeMillis());
        Logger.i(this.getLocalClassName(),"去设置时间");
    }

    private void showContentWindow() {
        Logger.i(this.getLocalClassName(),"去设置地址");
    }

    private void showTypeWindow() {
        Logger.i(this.getLocalClassName(),"去设置类型");
        taskInfo.setType("type1");
        SelectTaskTypeFragment fragment = SelectTaskTypeFragment.newInstance("","");
        FragmentManager fm = getFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        Intent intent = new Intent(this, SelectTaskTypeActivity.class);
        startActivityForResult(intent,RESULT_FIRST_USER);

    }

    private void cancel() {
        Logger.i(this.getLocalClassName(), "取消退出");
    }

    private void saveTask(){
        //检查填写情况
        checkInput();
        //整理保存数据
        taskInfo.setNote(memo_tv.getText().toString());
        taskInfo.setTitle(mTitle.getText().toString());
        taskInfo.setAddress(mContext.getText().toString());
        if (taskInfo.getStart_at() == null){
            taskInfo.setStart_at(SystemClock.currentThreadTimeMillis());
        }

        if(taskInfo.getEnd_at() == null){
            //默认是一整天
            taskInfo.setEnd_at(null);
        }

        taskInfo.setDone(false);
        saveTask(taskInfo);

    }

    private void checkInput(){
        if(TextUtils.isEmpty(taskInfo.getTitle())){
            //标题空

        }
        if(TextUtils.isEmpty(taskInfo.getType())){
            //类型空
        }
        //其他默认值
    }

    @Override
    public void saveTask(Task task) {
        presenter.saveNewTask(task);
    }

    @Override
    public void showMessage(String text) {
        Toast.makeText(this, text,Toast.LENGTH_LONG).show();
    }
}
